import requests

from business.jiaMiPass import jiaMiPass


class Httprequest(requests.Session):
    def __init__(self, u_token=None):
        super().__init__()
        self.token = u_token

    def get(self, url, params=None, headers=None, **kwargs):
        # 使用父类的 get 方法发送请求
        response = super(Httprequest,self).get(url, params=params, headers=headers, verify=False, **kwargs)
        return response

    def post(self, url, data=None, json=None, params=None, headers=None, **kwargs):
        # 使用父类的 post 方法发送请求
        response = super(Httprequest,self).post(url, data=data, json=json, params=params, headers=headers, verify=False, **kwargs)
        return response



class LoginOpionsApi():


    """
    https://172.17.21.248:9443/ugreen/v1/verify/check?token=    获取加密公钥的
    https://172.17.21.248:9443/ugreen/v1/verify/login     登录接口
    """
    def __init__(self):
        self.username="nas"
        self.password="Qwer1234"
        self.rsa_token=None
    def get_RsaToken(self,username):
        """
        https://172.17.21.248:9443/ugreen/v1/verify/check?token=
        :param username:
        :return:
        """
        url = "https://172.17.21.248:9443/ugreen/v1/verify/check?token="
        jsondata={
            "username":username
        }
        r = Httprequest().post(url,json=jsondata)
        if r.status_code == 200:
            # result.headers.get('X-Rsa-Token') if self._is_request_successful(result) else None
            print(r.headers.get('X-Rsa-Token'))
            self.rsa_token = r.headers.get('X-Rsa-Token')
            return self.rsa_token

    def login(self):
        url = "https://172.17.21.248:9443/ugreen/v1/verify/login"
        password=jiaMiPass().getpassword(self.password,self.get_RsaToken(self.username))
        jsiondata={
            "username":self.username,
            "password":password
        }
        r=Httprequest().post(url,json=jsiondata)
        if r.status_code == 200:
            token=r.json().get('data').get('token')
            return token


class UserOptionApi():
    def __init__(self, u_token):
        self.http = Httprequest(u_token)
        self.rsa_token=None
        self.username="nas"
        self.u_token=u_token

    def get_userlist(self,url, **kwargs):
        """
        获取用户配置项   https://172.17.21.248:9443/ugreen/v1/user/list?reverse=false
        这里有参数了，所以后面跟需要用parmeter的语法来，--》多个参数要用&
        """
        if self.u_token is None:
            print("请先登录！！！")
            return
        url=url +"&"+"token="+self.u_token
        r = self.http.get(url)
        print(len(r.json().get("data").get("list")))
        return r.json().get("data").get("list")


    def get_RsaToken(self,url,**kwargs):
        """
        设置用户配置项
        """
        jsiondata={
             "username":self.username
        }
        r = self.http.post(url,json=jsiondata)
        self.rsa_token = r.headers.get('X-Rsa-Token')
        return self.rsa_token


    def Jiaoyanmima(self,url,username,password):
        print(self.rsa_token)
        password=jiaMiPass().getpassword(password,self.rsa_token)
        jsiondata={
            "username":username,
            "password":password
        }
        if self.u_token is None:
            print("请先登录！！！")
            return
        url=url +"?token="+self.u_token
        r=self.http.post(url,json=jsiondata)
        if r.status_code == 200:
            return


    def createUser(self,url,username,password,email="",description="",role="admin",groups=[],enable_home_dir=False,deny_change_pwd=False,dir_quota=0,unit=1,**kwargs):
        """
        {"username":"999","email":"",
        "description":"","role":"admin",
        "groups":[],"enable_home_dir":false,
        "deny_change_pwd":false,"dir_quota":0,"unit":1,
        "password":"di8MGTIaTsb54+0NVjVOxJGsJXT7dEYtIkNKljkSC4GqEYZcAhw6mxhe6EWK9pM5SYxqcjQD39pMpP+ZigwHrcCMcnYS3+THfilvUtdiqtVP4/JStLTuzRemFL8x7xUCjamYr0GZscKCAod/67piRtubctu46Y08mILMbTcJTNzdBEYYLQcxJ/xEaapp8fZwYsgmX7xjKKYXWIWgULP7AZVGXp4a1rILrq4Uz/Ib46uJ7dmYBSRbeiBtUi7eb8Ta/YPO9H9PXdI+PTzFgKQX14NfcOb9o1nsKJc/FgZqaaKqVbeMcLTjXoKXE0i7RLXmMamh9q+Y/b8NvfAEGsm+0w=="}
        """
        password=jiaMiPass().getpassword(password,self.rsa_token)
        jsiondata={
            "username":username,
            "email":email,
            "description":description,
            "role":role,
            "groups":groups,
            "enable_home_dir":enable_home_dir,
            "deny_change_pwd":deny_change_pwd,
            "dir_quota":dir_quota,
            "unit":unit,
            "password":password
        }
        if self.u_token is None:
            print("请先登录！！！")
            return
        url=url +"?token="+self.u_token
        r = self.http.get(url)
        r=self.http.post(url,json=jsiondata)
        return r.status_code

